package com.sljy.hzk.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import com.sljy.hzk.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProgressView extends View {
    private int right;
    private int bottom;
    private Paint paint;
    private Paint paintC;
    private RectF rectF;
    private boolean mIsRunning = false;
    private float baseSpeed = 0.9f;
    private int speed = 30;
    private int angle = 0;
    private int maxAngele = 0;
    private String tag = "ProgressView";
    private boolean isForword = true;//前进
    private long time = 0;
    private float r = 0;
    private FocusEffct.OnTouchListener listener;

    public ProgressView(Context context) {
        super(context);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        rectF = new RectF(0,0,right,bottom);
        paint = new Paint();
        paintC = new Paint();
        setPaint(paint, Color.BLUE,0);
        setPaint(paintC,Color.WHITE,0);
    }

    private void setPaint(Paint p, int color ,int stroke ){
        p.setAntiAlias(true);
        p.setColor(color);
        p.setStyle(Paint.Style.FILL);
        p.setStrokeJoin(Paint.Join.ROUND);
        p.setPathEffect(new CornerPathEffect(60));
        p.setStrokeCap(Paint.Cap.ROUND);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i(tag,"onDraw");
        if(mIsRunning){
           // Log.i(tag, (System.currentTimeMillis()-time)+"___"+r);
            angle+=10;
            if(angle >= maxAngele){
                mIsRunning = false;
                angle = maxAngele;
                //回调
                listener.onEnd();
            }
        }
        canvas.drawArc(rectF,-90,angle,true, paint);
        canvas.drawCircle(bottom/2,bottom/2,bottom*0.42f,paintC);
        if(mIsRunning)
            postInvalidateDelayed(speed);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.i(tag,"onSizeChanged");
        right = w;
        bottom = h;
        rectF.set(0,0,right,bottom);
    }

    private Runnable mCreateRect = new Runnable() {
        @Override
        public void run() {
            if (mIsRunning) {

            }
        }
    };

    public void start(int maxAngele) {
        if (!mIsRunning) {
            this.maxAngele = maxAngele;
            angle = 0;
            mIsRunning = true;
            postInvalidate();
        }
    }

    /**
     * 停止
     */
    public void stop() {
        mIsRunning = false;
    }

    public void OnEffctEnd(FocusEffct.OnTouchListener l){
        listener = l;
    }

    public interface OnTouchListener{
        boolean onEnd();
    }

}
